package com.dragon.test.hibernate.onetomany.action;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.dragon.test.hibernate.onetomany.bean.Customer;
import com.dragon.test.hibernate.onetomany.bean.LinkMan;

/**
 * 
 * @author SmallDragon
 * 
 *         测试hibernate一对多级联操作
 *
 */
public class OneToMany {

	public static void main(String[] args) {
		Configuration configuration = new Configuration();
		configuration.configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();

		session.close();
		sessionFactory.close();
	}

	/**
	 * 级联添加,no cascade
	 */
	@Test
	public void testSave() {
		Configuration configuration = new Configuration();
		configuration.configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		// 1 新建实体
		Customer customer = new Customer();
		customer.setCustName("百度");
		customer.setCustLevel("vip");
		customer.setCustSource("网络");
		customer.setCustPhone("123456");

		LinkMan linkMan = new LinkMan();
		linkMan.setLkmName("张三");
		linkMan.setLkmGender("男");
		linkMan.setLkmPhone("911");

		// 2 实体类之间建立关系
		customer.getLinkMans().add(linkMan);
		linkMan.setCustomer(customer);

		// 3 保存
		session.save(customer);
		session.save(linkMan);

		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	/**
	 * 级联保存简化写法,cascade
	 */
	@Test
	public void testSave2() {
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction transaction = null;
		try {
			Configuration configuration = new Configuration();
			configuration.configure();
			sessionFactory = configuration.buildSessionFactory();
			session = sessionFactory.getCurrentSession();
			transaction = session.beginTransaction();

			// 1 新建实体
			Customer customer = new Customer();
			customer.setCustName("阿里");
			customer.setCustLevel("svip");
			customer.setCustSource("网络");
			customer.setCustPhone("145646");

			LinkMan linkMan = new LinkMan();
			linkMan.setLkmName("小马");
			linkMan.setLkmGender("男");
			linkMan.setLkmPhone("653674567");

			// 2 实体类之间建立关系
			customer.getLinkMans().add(linkMan);
			linkMan.setCustomer(customer);

			// 3 级联保存
			session.save(customer);
			//session.save(linkMan);

			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.close();
			sessionFactory.close();
		}
	}

	/**
	 * 级联删除
	 */
	@Test
	public void testDelete() {
		Configuration configuration = new Configuration();
		configuration.configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		Customer customer = session.get(Customer.class, 6);
		session.delete(customer);

		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	/**
	 * 修改 inverse
	 */
	@Test
	public void testUpdate() {
		Configuration configuration = new Configuration();
		configuration.configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		Customer customer = session.get(Customer.class, 3);
		LinkMan linkMan = session.get(LinkMan.class, 1);

		customer.getLinkMans().add(linkMan);
		linkMan.setCustomer(customer);

		transaction.commit();
		session.close();
		sessionFactory.close();
	}

}
